/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package la.foton.homebroker.negocio.validador;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author luiz
 */
public class Validador {

    public static void valida(Object entidade) throws ViolacaoNegocioException {
        for (Field field : entidade.getClass().getDeclaredFields()) {
            field.setAccessible(true);
            for (Annotation annotation : field.getAnnotations()) {
                Class<?> annontationType = annotation.annotationType();
                if (annontationType.isAnnotationPresent(ImplementacaoProcessadorValidacao.class)) {
                    ImplementacaoProcessadorValidacao ipv = annontationType.getAnnotation(ImplementacaoProcessadorValidacao.class);
                    try {
                        ProcessadorValidacao processador = ipv.value().newInstance();
                        processador.readAnnotation(annotation);
                        processador.valida(field.get(entidade));
                        
                    } catch (InstantiationException ex) {
                        Logger.getLogger(Validador.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IllegalAccessException ex) {
                        Logger.getLogger(Validador.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
    }
}
